package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import Models.POSProductServingType;

public class POSProductServingTypePresenter extends AbstractPresenter<POSProductServingType> {
	final String SINGLEITEM = "posProductServingType";
	final String MULTIITEM = "pOSProductServingTypes";
	
	public ArrayList<POSProductServingType> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public POSProductServingType tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public ArrayList<POSProductServingType> getListFromJSONArray(JSONObject jsonObj){
		ArrayList<POSProductServingType> psts = new ArrayList<POSProductServingType>();
		try{
			if(!jsonObj.isNull("posProductServingType")){
					Object item = jsonObj.get("posProductServingType");
					if(item instanceof JSONArray){
						JSONArray jArray = (JSONArray)item;
						for(int i=0; i < jArray.length(); i++){
							JSONObject jObject = jArray.getJSONObject(i);
							psts.add(createItemFromJSONObject(jObject));
						}
					} else {
						JSONObject jObject = (JSONObject)item;
						psts.add(createItemFromJSONObject(jObject));
					}
				}
			return psts;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSProductServingType createItemFromJSONObject(JSONObject jsonPST){
		POSProductServingType pst = new POSProductServingType();
		try{
			pst.setId(jsonPST.getInt("id"));
			pst.setCost(jsonPST.getDouble("stCost"));
			//Build Beer
			if(!jsonPST.isNull("servingTypeBeer")){
				POSBeerViewPresenter beerPresenter = new POSBeerViewPresenter();
				JSONObject jsonBeer = jsonPST.getJSONObject("servingTypeBeer");
				pst.setBeer(beerPresenter.createItemFromJSONObject(jsonBeer));
			} else {
				pst.setBeer(null);
			}
			//Build Glassware
			POSGlassWarePresenter glasswarePresenter = new POSGlassWarePresenter();
			JSONObject jsonGlassware = jsonPST.getJSONObject("glassware");
			pst.setGlassWare(glasswarePresenter.createItemFromJSONObject(jsonGlassware));
			return pst;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(POSProductServingType item){
		try{
			JSONObject json = new JSONObject();
			json.put("id", item.getId());
			json.put("stCost", item.getCost());
			if(item.getBeer()!=null){
				POSBeerViewPresenter beerPresenter = new POSBeerViewPresenter();
				json.put("servingTypeBeer", beerPresenter.createJSON(item.getBeer()));
			} else {
				json.put("servingTypeBeer", JSONObject.NULL);
			}
			POSGlassWarePresenter glassPresenter = new POSGlassWarePresenter();
			json.put("glassware", glassPresenter.createJSON(item.getGlassWare()));
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSProductServingType getEmptyEntity(){
		POSProductServingType entity = new POSProductServingType();
		return entity;
	}
}
